﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    class CommonUtils
    {
        public static Boolean hourNumValidate(System.Windows.Forms.TextBox hourBox)
        {
            int value = 0;
            try
            {
                value = int.Parse(hourBox.Text);
            }
            catch (FormatException)
            {
                value = 0;
                return false;
            }
            if (value < 0)
            {
                hourBox.Text = "0";
            }
            if (value > 23)
            {
                hourBox.Text = "23";
            }
            return true;
        }

        public static Boolean minuteNumValidate(System.Windows.Forms.TextBox hourBox)
        {
            int value = 0;
            try
            {
                value = int.Parse(hourBox.Text);
            }
            catch (FormatException)
            {
                value = 0;
                return false;
            }
            if (value < 0)
            {
                hourBox.Text = "0";
            }
            if (value > 59)
            {
                hourBox.Text = "59";
            }
            return true;
        }

        public static void numberValueHandle(params System.Windows.Forms.TextBox[] textBoxs)
        {
            if (textBoxs.Length > 0)
            {
                foreach (System.Windows.Forms.TextBox textBox in textBoxs)
                {
                    int value = int.Parse(textBox.Text);
                    if (value < 10)
                    {
                        textBox.Text = "0" + value;
                    }
                }
            }
        }

        public static void shutdown()
        {
            Process shutdownProc = new Process(); 
            shutdownProc.StartInfo.FileName = "cmd.exe"; 
            shutdownProc.StartInfo.UseShellExecute = false;
            shutdownProc.StartInfo.RedirectStandardInput = true;
            shutdownProc.StartInfo.RedirectStandardOutput = true;
            shutdownProc.StartInfo.RedirectStandardError = true; 
            shutdownProc.StartInfo.CreateNoWindow = true; 
            shutdownProc.Start(); 
            shutdownProc.StandardInput.WriteLine("shutdown -s -t 00");
        }

        public static Dictionary<String, int> getCountdownMap(int distTime)
        {
            Dictionary<String, int> resultMap = new Dictionary<String, int>();
            int day = ((distTime / 1000) / (3600 * 24));
            int hour = ((distTime / 1000) - day * 86400) / 3600;
            int minutes = ((distTime / 1000) - day * 86400 - hour * 3600) / 60;
            int seconds = (distTime / 1000) - day * 86400 - hour * 3600 - minutes * 60;
            resultMap.Add("day", day);
            resultMap.Add("hour", hour);
            resultMap.Add("minutes", minutes);
            resultMap.Add("seconds", seconds);
            return resultMap;
        }

    }
}
